import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { AssetSite } from "@/api/backend/index";
import { handleTree } from "@/utils/common";
import qs from "qs";

export const useAssetSite = defineStore(Names.AssetSite, {
  state: () => {
    return {
      single: <any>{},
      type: <any>[],
      tags: <any>{},
      originData: <any>[], //未处理过后的站点列表
      siteForAnalysis: <any>{},
      defaultCheckedKeys: <any>[], //默认选中节点
      defaultCheckedNodes: <any>{}, //默认选中节点
      AllCheckedKeys: <any>{}, //全部节点
      AllCheckedNodes: <any>{}, //全部节点
    };
  },
  getters: {},
  actions: {
    //获取站点管理列表数据 过滤
    getAssetSiteList(filter: any) {
      return new Promise((resolve, reject) => {
        _get(AssetSite + "/oldlist", filter, "json", "")
          .then((res: any) => {
            this.originData = res.rows; //原始树形结构数据
            let data = handleTree(res.rows, "siteId", "", "");
            resolve(data);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },

    //处理不同类型是否可选
    getDisabledData(data: any, disabled: any, boo: boolean) {
      return new Promise((resolve, reject) => {
        let len = data.length;
        for (let i = 0; i < len; i++) {
          if (data[i].siteType == disabled) {
            data[i].disabled = boo ? false : true;
          } else {
            data[i].disabled = boo ? true : false;
          }
        }
        resolve(data);
      });
    },
    //过滤多类型
    getAssetSiteTypes(filter: any, disable: any, boo: boolean) {
      return new Promise((resolve, reject) => {
        _get(
          AssetSite +
            "/oldlist" +
            "?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then((result: any) => {
            this.defaultCheckedKeys.push(result.rows[0].siteId);
            this.defaultCheckedNodes = result.rows[0];
            this.originData = result.rows; //原始树形结构数据
            if (JSON.stringify(disable) !== "{}") {
              this.getDisabledData(result.rows, disable.disabled, boo).then(
                (res: any) => {
                  let data = handleTree(res, "siteId", "", "");
                  resolve(data);
                }
              );
            } else {
              let data = handleTree(result.rows, "siteId", "", "");
              resolve(data);
            }
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    getAllChoose(filter: any, disable: any, boo: boolean) {
      return new Promise((resolve, reject) => {
        _get(
          AssetSite +
            "/oldlist" +
            "?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then((result: any) => {
            this.AllCheckedNodes = result.rows;
            console.log("getAllChoose", result);
            this.originData = result.rows; //原始树形结构数据
            if (JSON.stringify(disable) !== "{}") {
              this.getDisabledData(result.rows, disable.disabled, boo).then(
                (res: any) => {
                  let data = handleTree(res, "siteId", "", "");
                  resolve(data);
                }
              );
            } else {
              let data = handleTree(result.rows, "siteId", "", "");
              resolve(data);
            }
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //获取站点管理数据进行树型再造
    getOrigin() {
      return new Promise((resolve, reject) => {
        _get(AssetSite + "/oldlist", {}, "json", "")
          .then((res: any) => {
            let data = handleTree(res.rows, "siteId", "", "");
            console.log("获取站点管理数据进行树型再造", data);
            resolve(data);
          })
          .catch((err: any) => {
            console.log(err);
            reject(err);
          });
      });
    },
    // 获取站点数据(过滤)
    getFilterList(filter: any) {
      return new Promise((resolve, reject) => {
        _get(AssetSite + "/oldlist", filter, "json", "")
          .then(async (res: any) => {
            console.log("获取站点数据(过滤)", res);
            resolve(res.rows);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //使用id过滤站点
    getfilterById(filter: any) {
      return new Promise((resolve, reject) => {
        _get(AssetSite + `/${filter.siteId}`, {}, "json", "")
          .then(async (res: any) => {
            console.log("使用id过滤站点", res);
            resolve(res.data);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //删除站点
    delete(filter: any) {
      console.log("删除站点", filter);
      return new Promise((resolve, reject) => {
        _delete(AssetSite + "/" + filter.id, {}, "json", "")
          .then(async (res) => {
            console.log("删除站点", res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    // 新增站点数据仓库
    addSite(params: any) {
      return new Promise((resolve, reject) => {
        _post(AssetSite, params, "json", "")
          .then(async (res: any) => {
            console.log("新增站点数据仓库", res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //修改站点数据
    editSite(params: any) {
      return new Promise((resolve, reject) => {
        _put(AssetSite, params, "json", "")
          .then(async (res: any) => {
            console.log("修改站点数据", res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
